package com.AUI.components
{
	import com.AUI.*;
	import com.AUI.define.style.*;
	import com.AUI.events.*;
	
	import flash.events.*;
	import flash.text.*;
	/**
	 * @eventType com.AUI.events.AInputEvent#ENTER
	 */
	[Event(name="enter", type="com.AUI.events.AInputEvent")]
	/**
	 * Input.
	 * @author HuangMinPing
	 * @site www.AminLab.com
	 */
	public class Input extends AComponent
	{
		/**
		 * [element] text.
		 */
		public var e_text:TextField = new TextField;
		/**
		 * Constructor.
		 */
		public function Input(text:String = '')
		{
			// init attributes
			depth = 1;
			// init elements
			e_text.type = TextFieldType.INPUT;
			e_text.text = text;
			e_text.width  = 100;
			e_text.height = 18;
			e_text.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown,false,0,true);
			elements.addChild(e_text);
			
			/**************** INITCOMPLETE ****************/
			Object(this).constructor == Input && initComplete();
		}
		/**
		 * Get or set the text of the label.
		 */
		override public function set value(variable:*):void
		{
			var text:String = variable as String;
			e_text.text = text;
		}
		override public function get value():*
		{
			return e_text.text;
		}
		/**
		 * Display as password.
		 */
		public function set displayAsPassword(value:Boolean):void
		{
			e_text.displayAsPassword = value;
		}
		public function get displayAsPassword():Boolean
		{
			return e_text.displayAsPassword;
		}
		/**
		 * Length.
		 */
		public function get length():int
		{
			return e_text.length;
		}
		/**
		 * Max length.
		 */
		public function set maxLength(value:int):void
		{
			e_text.maxChars = value;
		}
		public function get maxLength():int
		{
			return e_text.maxChars;
		}
		/**
		 * @private
		 */
		override protected function updateDrawStyle():void
		{
			super.updateDrawStyle();
			// update text
			var font:FONT = drawStyle.font; 
			var textFormat:TextFormat = new TextFormat(font.family,font.size,font.color,font.bold,font.italic,font.underline,null,null,font.align);
			e_text.defaultTextFormat = textFormat;
			e_text.setTextFormat(textFormat);
		}
		/**
		 * @private
		 */
		override protected function resizeBoxModel():void
		{
			// done
			super.resizeBoxModel();
			e_text.width  = boxModel.content.width;
			e_text.height = boxModel.content.height;
		}
		/**
		 * @private
		 */
		protected function onKeyDown(e:KeyboardEvent):void
		{
			if(e.keyCode == 13)
			{
				var event:AInputEvent = new AInputEvent(AInputEvent.ENTER);
				this.dispatchEvent(event);
			}
		}
	}
}